package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.tool.ToolClient;
import com.fwtai.web.ConsultStatisticsDao;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 电话咨询律师统计业务层
 * @作者 李宇鹏
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2023-02-28 09:28:02
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ConsultStatisticsService{

  private final ConsultStatisticsDao consultStatisticsDao;

  private final ThreadPoolTaskExecutor taskExecutor;

  public ConsultStatisticsService(final ConsultStatisticsDao consultStatisticsDao,final ThreadPoolTaskExecutor taskExecutor){
    this.consultStatisticsDao = consultStatisticsDao;
    this.taskExecutor = taskExecutor;
  }

  public String queryById(final Long kid){
    return CompletableFuture.supplyAsync(()->{
      if(kid == null){
        return ToolClient.jsonValidateField();
      }
      return ToolClient.queryJson(consultStatisticsDao.queryById(kid));
    },taskExecutor).join();
  }

  public String listData(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null){
          return ToolClient.jsonValidateField();
        }
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> consultStatisticsDao.getListData(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> consultStatisticsDao.getListTotal(formData),taskExecutor);
        CompletableFuture.allOf(listData,listTotal).join();
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }

  public String getLawyerRecord(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null){
          return ToolClient.jsonValidateField();
        }
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> consultStatisticsDao.getLawyerRecord(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> consultStatisticsDao.getLawyerRecordTotal(formData),taskExecutor);
        CompletableFuture.allOf(listData,listTotal).join();
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }

  public String getUserRecord(final PageFormData formData){
    return CompletableFuture.supplyAsync(()->{
      try{
        if(ToolClient.dataTableMysql(formData) == null){
          return ToolClient.jsonValidateField();
        }
        final CompletableFuture<List<HashMap<String,Object>>> listData = CompletableFuture.supplyAsync(() -> consultStatisticsDao.getUserRecord(formData),taskExecutor);
        final CompletableFuture<Integer> listTotal = CompletableFuture.supplyAsync(() -> consultStatisticsDao.getUserRecordTotal(formData),taskExecutor);
        CompletableFuture.allOf(listData,listTotal).join();
        return ToolClient.dataTableOK(listData.join(),listTotal.join());
      } catch (final Exception ignored){
        return ToolClient.dataTableException();
      }
    },taskExecutor).join();
  }
}